﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MockPrjTeam4.Entity;
using MockPrjTeam4.Biz;
using MockPrjTeam4.Common;

namespace MockPrjTeam4
{
    public partial class OrganisationAdd : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            string popup = @"<script>
            function popup(url, width, height)
            {
                //var width = 500;
                //var height = 400;
                var left = (screen.width - width) / 2;
                var top = (screen.height - height) / 2;
                var params = 'width=' + width + ', height=' + height;
                params += ', top=' + top + ', left=' + left;
                params += ', directories=no';
                params += ', location=no';
                params += ', menubar=no';
                params += ', resizable=no';
                params += ', scrollbars=no';
                params += ', status=no';
                params += ', toolbar=no';
                newwin = window.open(url, 'windowname5', params);
                if (window.focus) 
                { 
                    newwin.focus() 
                }
                return false;
            }
            </script>";
            if (!Page.ClientScript.IsStartupScriptRegistered("Mast"))
            {
                Page.ClientScript.RegisterStartupScript(this.GetType(),
                    "Mast", popup);
            }
            lstProgrammes.DataSource = ProgrammeBiz.GetListProgrammeName();
            lstProgrammes.DataBind();
            lstServices.DataSource = ServiceBiz.GetListServiceName();
            lstServices.DataBind();
        }


        protected void btnSave_Click(object sender, EventArgs e)
        {
            Organisation org = new Organisation();
            org.OrganisationName = txtOrgName.Text;
            org.ShortDescription = txtOrgShortDes.Text;
            org.AddressId = Methods.ConvertNumber(hdfAddressId.Value);
            org.BusinessId = Methods.ConvertNumber(hdfTypeOfBusinessId.Value);
            org.PhoneNumber = txtPhoneNumber.Text;
            org.TownId = Methods.ConvertNumber(hdfTownId.Value);
            org.CountyId = Methods.ConvertNumber(hdfCountyId.Value);
            org.Countryid = Methods.ConvertNumber(hdfCountryId.Value);
            if (chkPreOrg.Checked) org.PreferredOrganisation = true;
            else org.PreferredOrganisation = false;
            if (hdfContact.Value == "")
                org.ContactId = -1;
            else
                org.ContactId = Methods.ConvertNumber(hdfContact.Value);
            org.FullDescription = txtOrgFullDes.Text;
            org.Fax = txtFax.Text;
            org.Email = txtEmail.Text;
            org.WebAddress = txtWebAdd.Text;
            org.CharityNumber = txtCharityNumber.Text;
            org.CompanyNumber = txtCompanyNumber.Text;
            if (chkIsActive.Checked) org.IsActive = true;
            else org.IsActive = false;
            org.OrganisationSpecialism = "";
            org.ServiceDisabilities = "";
            org.ServiceBarriers = "";
            org.ServiceBenefits = "";
            org.ServiceCircumstances = "";
            org.ServiceEthnicity = "";
            org.Accreditation = "";
            for (int i = 0; i < cblServiceDisabilitiesCapabilities.Items.Count; i++)
            {
                if (cblServiceDisabilitiesCapabilities.Items[i].Selected)
                {
                    org.ServiceDisabilities = cblServiceDisabilitiesCapabilities.Items[i].Text;
                }
            }
            for (int i = 0; i < cblServiceBarriersCapabilities.Items.Count; i++)
            {
                if (cblServiceBarriersCapabilities.Items[i].Selected)
                {
                    org.ServiceBarriers = cblServiceBarriersCapabilities.Items[i].Text;
                }
            }
            for (int i = 0; i < cblServiceBenifitsCapabilities.Items.Count; i++)
            {
                if (cblServiceBenifitsCapabilities.Items[i].Selected)
                {
                    org.ServiceBenefits = cblServiceBenifitsCapabilities.Items[i].Text;
                }
            }
            for (int i = 0; i < cblServicePersonalCircumstancesCapabilities.Items.Count; i++)
            {
                if (cblServicePersonalCircumstancesCapabilities.Items[i].Selected)
                {
                    org.ServiceCircumstances = cblServicePersonalCircumstancesCapabilities.Items[i].Text;
                }
            }
            for (int i = 0; i < cblServiceEthinicityCapabilities.Items.Count; i++)
            {
                if (cblServiceEthinicityCapabilities.Items[i].Selected)
                {
                    org.ServiceEthnicity = cblServiceEthinicityCapabilities.Items[i].Text;
                }
            }
            for (int i = 0; i < cblAccreditation.Items.Count; i++)
            {
                if (cblAccreditation.Items[i].Selected)
                {
                    org.Accreditation = cblAccreditation.Items[i].Text;
                }
            }

            Organisation orgName = OrganisationBiz.SearchOrgName(txtOrgName.Text);
            if (orgName == null)
            {
                OrganisationBiz.AddOrganisation(org);
                lblMessage.Text = "You added " + txtOrgName.Text + " successfully";
            }
            else
            {
                lblMessage.Text = txtOrgName.Text + " had in DB. Enter again";
            }
        }

        protected void btnBack_Click(object sender, EventArgs e)
        {
            Response.Redirect("../Views/ListOrganisation.aspx");
        }

        protected void chkExpInt_CheckedChanged(object sender, EventArgs e)
        {
            if (chkExpInt.Checked)
            {
                tpDetails3.Visible = true;
                tcDep.ActiveTab = tpDetails3;
            }
            else
            {
                tpDetails3.Visible = false;
            }
        }

        
    }
}